//
//  MYShopCarCell.swift
//  MuYing
//
//  Created by tbb on 2019/7/30.
//  Copyright © 2019 TBB. All rights reserved.
//specification

import UIKit

@objc protocol MYShopCarCellDelegate:NSObjectProtocol {
    
    func didSelectGoodAction(_ indexPath:NSIndexPath);

}

class MYShopCarCell: YQTableViewBaseCell,UITextFieldDelegate {
    
    @objc var indexPath:NSIndexPath?
    
    @objc var orderModel = YQPExchangeIntegerModel()
    
    @objc var cartModel = YQPCartModel()

    @objc weak var delegate: MYShopCarCellDelegate?

    @objc @IBOutlet weak var lineView: UIView!
    
    @objc var isCarCell = true {
        didSet {
            if !isCarCell {
                self.countTextField.isHidden = !isCarCell;
                self.reductionBtn.isHidden = !isCarCell;
                self.addBtn.isHidden = !isCarCell;
                
                self.countLb.isHidden = isCarCell;
            }
        }
    }

    @IBOutlet weak var countLb: UILabel!
    @IBOutlet weak var selectBtn: UIButton!
    @IBOutlet weak var goodsIcon: UIImageView!
    
    @IBOutlet weak var titleLb: UILabel!
    

    @IBOutlet weak var specificationLb: UILabel!
    
    @IBOutlet weak var priceLb: UILabel!
    
    @IBOutlet weak var reductionBtn: UIButton!
    
    @IBOutlet weak var addBtn: UIButton!
    
    @IBOutlet weak var countTextField: UITextField!
    
    override func awakeFromNib() {
        super.awakeFromNib()
        self.lineView.isHidden = true;
        self.countTextField.keyboardType = .numberPad;
        self.countTextField.delegate = self;
        self.selectBtn.setImage(UIImage(named: "shopCar_select_d_icon"), for: .normal)
        self.selectBtn.setImage(UIImage(named: "shopCar_select_s_icon"), for: .selected)
        
        self.goodsIcon.layer.cornerRadius = 6;
        self.goodsIcon.clipsToBounds = true;
        
        
    }

    @IBAction func reductionAction(_ sender: UIButton) {
        let text = self.countTextField.text as NSString?;
        guard var num = text?.integerValue else {
            return;
        }
        
        if num < 2 {
            return;
        }
        num = num - 1;
        let numStr = String(num);
        self.countTextField.text = numStr;
    }
    
    @IBAction func addAction(_ sender: UIButton) {
        let text = self.countTextField.text as NSString?;
        guard var num = text?.integerValue else {
            return;
        }
        num = num + 1;
        let numStr = String(num);
        self.countTextField.text = numStr;
    }
    override func setSelected(_ selected: Bool, animated: Bool) {
        super.setSelected(selected, animated: animated);
    }
    
    @IBAction func selectAcaion(_ sender: UIButton) {
        if let delegate = self.delegate, delegate.responds(to: Selector(("didSelectGoodAction:"))),let indexPath = self.indexPath {
            self.delegate?.didSelectGoodAction(indexPath);
        }
    }
    //UITextFieldDelegate
    func textFieldDidEndEditing(_ textField: UITextField) {
        let text = textField.text;
        let nsstringStr = text as NSString?
        
        if let num = nsstringStr?.integerValue , num < 1{
            textField.text = "1";
        }
    }
    
    @objc func setModel(_ model:YQPExchangeIntegerModel){
        self.orderModel = model
        self.titleLb.text = model.goodsName;
        self.specificationLb.text = kYQLSSwift("x" + "1");
        self.priceLb.text = kYQLSSwift("$" + model.totalAmount);
        self.isCarCell = false;
        //self.goodsIcon.kf.setImage(with: URL(string: model.defaultImage))
        self.goodsIcon.setImageWith(URL(string: model.defaultImage), placeholder: nil);
    }
    
    @objc func setGoodCartModel(_ cartModel:YQPCartModel){
        self.cartModel = cartModel
        self.titleLb.text = cartModel.skuName;
        self.specificationLb.text = kYQLSSwift("");
        self.priceLb.text = kYQLSSwift("$" + cartModel.price);
        self.isCarCell = false;
        //self.goodsIcon.kf.setImage(with: )
        self.goodsIcon.setImageWith(URL(string: cartModel.skuImg), placeholder: nil)
        self.countLb.text = cartModel.itemNumber;

    }
}
